<?php

//Hàm chứng thực dùng để login

class Admin_Models_LoginModel{
	function login($arrParam){
		$db = Zend_Db_Table::getDefaultAdapter();
	    $uname = trim($arrParam['username']);
	    $password = trim($arrParam['password']);
		if($uname == "" || $password == ""){
			return false;
		}else{
			$password = md5(md5($password) . ' ' . md5($arrParam['salt']));
		   	$auth = Zend_Auth::getInstance();
		
		   	$authAdapter = new Zend_Auth_Adapter_DbTable($db);
		    $authAdapter->setTableName('admin_user')
		    			->setIdentityColumn('username')
		    			->setCredentialColumn('password');
    		$authAdapter->setIdentity($uname);
    		$authAdapter->setCredential($password);
			$result = $auth->authenticate($authAdapter);
    		if($result->isValid()){
    			$data = $authAdapter->getResultRowObject(null,array('password','salt'));
    			$auth->getStorage()->write($data);
    			
    			require_once('Zend/Session/Namespace.php');
	            $session = new Zend_Session_Namespace('Zend_Auth');
	            // Set the time of user logged in
	            $session->setExpirationSeconds(7*24*3600);
	            
	            // If "remember" was marked
	            if ($arrParam['remember']) {
	                Zend_Session::rememberMe();
	                Zend_Session::start();
	            }
    			return true;
    			
    		}else{
    			return false;
    		}
    	}
	}
}